package com.windchime.blog.controller;

import cn.hutool.core.convert.Convert;
import com.windchime.blog.entity.Comment;
import com.windchime.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
public class FrontCommentController {

    @Autowired
    private CommentService commentService;


    /**
     * 评论段
     * @param blogId 博客ID
     * @param model 传值
     * @return comments
     */
    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model) {
        List<Comment> comments = commentService.listCommentByBlogId(blogId);

        model.addAttribute("comments", comments);
        return "/blog :: commentList";
    }

    /**
     * 添加评论
     * @param comment 对象
     * @param model 传值
     * @return blog
     */
    @PostMapping("/comments")
    public String post(Comment comment, Model model) {
        commentService.saveComment(comment);
        List<Comment> comments = commentService.listCommentByBlogId(Convert.toLong(comment.getBlogId()));
        model.addAttribute("comments", comments);
        return "blog :: commentList";
    }
}
